var focused;
var last_v;

function int_format( number, thousands ) {
    var arr = number.split('').reverse();
    var rev = new Array();
    for (var i=0; i<arr.length; i++)
    {
    	if (i>0 && i%3==0) rev.push(thousands);
    	rev.push(arr[i]);
    }
    return rev.reverse().join('');
}
function number_format(v, thousands, dec){
	
	var sign =  '';
	if (v.indexOf('-')==0)
	{
		sign = '-';
		v = v.slice(1);
	}
	if (v=='') return '';
	
	var dec_pos = v.indexOf(dec);
	var int_part = dec_pos>=0 ? v.substr(0, dec_pos) : v;
	if (thousands!='') while(int_part.indexOf(thousands)>=0) int_part = int_part.replace(thousands, '');
	int_part = int_part.length>0 ? parseInt(int_part)+'' : '0';
	
	var dec_part = dec_pos>=0 ? v.substr(dec_pos+1) : '';
	if (thousands!='') while(dec_part.indexOf(thousands)>=0) dec_part = dec_part.replace(thousands, '');
	dec_part = dec_part.indexOf(dec)>=0 ?
			dec_part.substr(0, dec_part.indexOf(dec)) :
			dec_part;
	
	v = dec_pos>=0 ? 
		sign + int_format(int_part, thousands) + dec + dec_part :
		sign + int_format(int_part, thousands);
	
	return v;
}

function myParseNum(jq)
{
	var v = jq.val();
	var d = jq.data('dec');
	var t = jq.data('thousands');
	var min = jq.data('min');
	var max = jq.data('max');
	
	var sign =  '';
	var dot =  '';
	var integer = '0';
	var dec = '';
	
	// get minus signs
	if (v.indexOf('-')==0)
	{
		sign = '-';
		v = v.slice(1);
	}
	if ( ! jq.data('integer'))
	{
		// dot at the end of the number is most likely not a problem!
		if (v.length>0 && v.indexOf('.')==v.length-1)
		{
			dot = d;
			v = v.slice(0, v.length-1);
		}
		
		var dp = v.indexOf(d);
		if (dp>-1)
		{
			integer = v.slice(0, dp)+'';
			dec = v.slice(dp+1)+'';
		}
		else integer = v;
	}
	
	v = integer.replace(/\D/g, '');
	dec = dec.replace(/\D/g, '');
	if (dec!='') v += d +dec;
	v = sign + v;
	
	if (min!='' && parseFloat(v)<min) v = min;
	else if (max!='' && parseFloat(v)>max) v = max;
	
	v = number_format(v, t, d)+dot;
	
	jq.val(v);
}
function delayKeyup(val, jq, t)
{
	setTimeout(
		function(){
			if (jq.val()==val) myParseNum(jq);
		},
		t);
}

function getSelectionStart(o) {
	if (o.createTextRange) {
		var r = document.selection.createRange().duplicate()
		r.moveEnd('character', o.value.length)
		if (r.text == '') return o.value.length
		return o.value.lastIndexOf(r.text)
	} else return o.selectionStart
}

$(document).ready(function() {
	$('.number input').each(function(){
		var rel = $(this).attr('rel');
		var tmp = typeof rel=='undefined' ? new Array() : rel.split('|');
		tmp[0] = typeof(tmp[0])=='undefined' ?  ',' : tmp[0];	//thousands
		tmp[1] = typeof(tmp[1])=='undefined' ?  '.' : tmp[1];	//dec
		tmp[2] = typeof(tmp[2])=='undefined' ?  '' : tmp[2];	//min
		tmp[3] = typeof(tmp[3])=='undefined' ?  '' : tmp[3];	//max
		$(this)
			.data('thousands', tmp[0])
			.data('dec', tmp[1])
			.data('integer', tmp[1]=='')
			.data('min', tmp[2])
			.data('max', tmp[3])
			.keyup(function(e){
				var pos = getSelectionStart($(this).get(0));
				var v = $(this).val();
				delayKeyup(v, $(this), pos==v.length ? 200 : 1000);
			});
		myParseNum($(this));
	});
});